﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using EntityObject;
using SharedLibraries;
using SharedLibraries.Constants;
using ThangNM_LIB.DB;
using ThangNM_LIB.FUNC;

namespace ThangNM.UserControl.Admin
{
    public partial class BackLinkUC : System.Web.UI.UserControl
    {
        public List<BackLinkEO> lstBL
        {
            get { return (List<BackLinkEO>)ViewState["lstBL"]; }
            set { ViewState["lstBL"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                Title_FormUC1.setTitle(ConstEx.Table_Name_C.BackLink, ConstEx.funcButtonToolBar.LIST);
                MsgUC1.setClearMsg();
                if (!IsPostBack)
                {
                    //if ((new ShareDAO.UserDAO()).SelectBy_sPermission(Session["secret"], Convert.ToString(Request.Url.AbsolutePath), ConstEx.funcButtonToolBar.MAIN) == false)
                    //{
                    //    ToolBarUC1.Visible = false;
                    //    grvList.Visible = false;
                    //    Commons.setAccessDenied(Page, MsgUC1);
                    //    return;
                    //}
                    ToolBarUC1.ViewGroupButtonByFunction(ConstEx.funcButtonToolBar.OTHER);
                    lstBL = new List<BackLinkEO>();
                    string sBackLink = System.IO.File.ReadAllText(Server.MapPath("~/App_Data/BackLink.xml"));
                    lstBL = (List<BackLinkEO>)XmlEx.Xml2ListObject(sBackLink, lstBL.GetType());
                    foreach (var item in lstBL)
                    {
                        item.sHtml = XmlEx.RollbackSpecialXMLCharater(item.sHtml);
                        item.sHomepage = XmlEx.RollbackSpecialXMLCharater(item.sHomepage);
                    }
                    BindData();
                }
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void EventButtonToolBar_Click(object sender, CustomerEventArgs e)
        {
            switch (e.iType)
            {
                case ConstEx.funcButtonToolBar.INSERT_LIST: break;
                case ConstEx.funcButtonToolBar.INSERT_ITEM: break;
                case ConstEx.funcButtonToolBar.UPDATE_ITEM:
                    SaveChange();
                    break;
                case ConstEx.funcButtonToolBar.DELETE_LIST: break;
                case ConstEx.funcButtonToolBar.DELETE_ITEM: break;
                case ConstEx.funcButtonToolBar.REFRESH_LIST: break;
                case ConstEx.funcButtonToolBar.EXPORT_EXCEL: break;
                case ConstEx.funcButtonToolBar.RESET_FORM: break;
            }
        }

        protected void SaveChange()
        {
            try
            {
                foreach (var item in lstBL)
                {
                    item.sHtml = "<a href=\"http://nguyenmanhthang.tk/\" target=\"_blank\">Nguyễn Mạnh Thắng</a>";
                    //item.sHtml = Commons.ReplaceSpecialXMLCharater(item.sHtml);
                    item.sHomepage = XmlEx.ReplaceSpecialXMLCharater(item.sHomepage);
                }
                System.IO.File.WriteAllText(Server.MapPath("~/App_Data/BackLink.xml"), XmlEx.ListObject2Xml(lstBL, ""));
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Cập nhật thành công.");
                BindData();
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        //protected List<BackLinkEO> GetListObject(GridView grv)
        //{
        //    List<BackLinkEO> lstobj = new List<BackLinkEO>();
        //    BackLinkEO obj;
        //    foreach (GridViewRow row in grv.Rows)
        //    {
        //        obj = new BackLinkEO();
        //        obj.PK_iBackLinkID = Convert.ToInt16(((Label)row.Cells[0].FindControl("lblPK_iBackLinkID")).Text);
        //        obj.sHtml = ((TextBox)row.Cells[1].FindControl("txtsHtml")).Text;
        //        obj.sHomepage = ((TextBox)row.Cells[2].FindControl("txtsHomepage")).Text;
        //        obj.iPriority = ((TextBox)row.Cells[3].FindControl("txtiPriority")).Text;
        //        obj.iStatus = Convert.ToInt16(((DropDownList)row.Cells[4].FindControl("ddliStatus")).SelectedValue);
        //        lstobj.Add(obj);
        //    }
        //    return lstobj;
        //}

        protected void btnInsert_Click(object sender, EventArgs e)
        {
            try
            {
                BackLinkEO objBL = new BackLinkEO();
                //TextBox txtPK_iBackLinkID_Temp = (TextBox)grvList.HeaderRow.FindControl("txtPK_iBackLinkID_Temp");
                TextBox txtsHtml_Temp = (TextBox)grvList.HeaderRow.FindControl("txtsHtml_Temp");
                TextBox txtsHomepage_Temp = (TextBox)grvList.HeaderRow.FindControl("txtsHomepage_Temp");
                TextBox txtiPriority_Temp = (TextBox)grvList.HeaderRow.FindControl("txtiPriority_Temp");
                DropDownList ddliStatus_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddliStatus_Temp");
                if (String.IsNullOrEmpty(txtiPriority_Temp.Text))
                {
                    BindData();
                    MsgUC1.setNotifications(ConstEx.FunctionSetMsg.WARNING, "Độ ưu tiên bắt buộc phải nhập");
                    txtiPriority_Temp.Focus();
                    return;
                }
                objBL.PK_iBackLinkID = Convert.ToInt16(lstBL.Count + 1);
                objBL.sHtml = txtsHtml_Temp.Text;
                objBL.sHomepage = OtherEx.getUrlStandard(txtsHomepage_Temp.Text);
                objBL.iPriority = Convert.ToInt16(txtiPriority_Temp.Text);
                objBL.iStatus = Convert.ToInt16(ddliStatus_Temp.SelectedValue);
                lstBL.Insert(0, objBL);
                //txtPK_iBackLinkID_Temp.Text = "";
                txtsHtml_Temp.Text = "";
                txtsHomepage_Temp.Text = "";
                txtiPriority_Temp.Text = "";
                ddliStatus_Temp.SelectedIndex = 0;
                BindData();
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Thêm thành công.");
            }
            catch (Exception ex)
            {
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.ERROR, ex.Message, ex.ToString());
            }
        }

        protected void BindData()
        {
            for (Int16 i = 0; i < lstBL.Count; i++)
            {
                lstBL[i].PK_iBackLinkID = Convert.ToInt16(i + 1);
            }
            grvList.DataSource = lstBL.ToList();
            grvList.DataBind();
            BindData_DDL();
        }

        protected void BindData_DDL()
        {
            DropDownList ddliStatus_Temp = (DropDownList)grvList.HeaderRow.FindControl("ddliStatus_Temp");
            if (ddliStatus_Temp != null)
            {
                ddliStatus_Temp.DataSource = GetListConstants.Category_bStatus_GLC();
                ddliStatus_Temp.DataValueField = "Key";
                ddliStatus_Temp.DataTextField = "Value";
                ddliStatus_Temp.DataBind();
            }

            CKEditor.NET.CKEditorControl txtsHtml_Temp = (CKEditor.NET.CKEditorControl)grvList.HeaderRow.FindControl("txtsHtml_Temp");
            if (txtsHtml_Temp != null)
            {
                txtsHtml_Temp.EnterMode = CKEditor.NET.EnterMode.BR;
                CKEditorEx.LoadCKEditor(txtsHtml_Temp);
            }
        }

        protected void grvList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdDelete")
            {
                int id = Convert.ToInt32(e.CommandArgument);
                lstBL.RemoveAt(id);
                BindData();
                MsgUC1.setNotifications(ConstEx.FunctionSetMsg.SUCCESS, "Xóa thành công.");
            }
        }

        protected void grvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddliStatus = (DropDownList)e.Row.FindControl("ddliStatus");
                Label lbliStatus = (Label)e.Row.FindControl("lbliStatus");
                ddliStatus.DataSource = GetListConstants.Category_bStatus_GLC();
                ddliStatus.DataValueField = "Key";
                ddliStatus.DataTextField = "Value";
                ddliStatus.DataBind();
                ddliStatus.SelectedValue = lbliStatus.Text;

                CKEditor.NET.CKEditorControl txtsHtml = (CKEditor.NET.CKEditorControl)e.Row.FindControl("txtsHtml");
                txtsHtml.EnterMode = CKEditor.NET.EnterMode.BR;
                CKEditorEx.LoadCKEditor(txtsHtml);
            }
        }
    }
}